package plugin

import (
	"database/sql"

	"gitee.com/mkwchecking/k-scaffold/config"
)

func (p *Plugins) GetSqlDB() (*sql.DB, error) {
	if p._sqldb != nil {
		return p._sqldb, nil
	}

	log, err := p.GetLogger()
	if err != nil {
		return nil, err
	}

	dbconf := config.Get().Database.Default

	db, cancel, err := initDB(p.Ctx(), dbconf.DatabaseConn, log.Logger)
	if err != nil {
		cancel()
		return nil, err
	}

	p._sqldb = db
	return p._sqldb, nil
}
